class AdminController < ApplicationController
  before_filter :user_authorize,:except =>[:getAllAirways,:getAllAirportComboBox,:errorInfoList,:getInternalAirways]
  layout "index",:except => [:getAllFuelTaxs,:fuel_tax,:addFuelTax,:editFuelTax,:getFuelTaxByID,
    :getAllPlaneTypes,:plane_tax,:addPlaneTax,:getPlaneTaxByID,:delPlaneTax,
    :editPlaneTax,:payment_type,:getAllPaymemtTypes,:editPaymentType,:getPaymentTypeByID,:delPosition,
    :delPaymentType,:addPaymentType,:customer_type,:getAllCustomerTypes,:delCustomerType,:editCustomerType,
    :getCustomerTypeByID,:delCustomerType,:addCustomerType,:phone_type,:getAllPhoneTypes,:delPhoneType,
    :editPhoneType,:getPhoneTypeByID,:delPhoneType,:addPhoneType,:card_type,:getAllCardTypes,:delCardType,
    :editCardType,:getCardTypeByID,:delCardType,:addCardType,:settlement_type,:getAllSettlementTypes,
    :delSettlementType,:editSettlementType,:getSettlementTypeByID,:delSettlementType,:addSettlementType,
    :cost_level,:getAllCostLevels,:delCostLevel,:editCostLevel,:getCostLevelByID,:delCostLevel,:addCostLevel,
    :airways,:getAllAirways,:delAirways,:editAirways,:getAirwaysByID,:delAirways,:addAirways,:airport,
    :getAllAirports,:delAirport,:editAirport,:getAirportByID,:delAirport,:addAirport,:department,:getAllDepartments,
    :delDepartment,:editDepartment,:getDepartmentByID,:addDepartment,:getCustomerTypes,:getCardTypes,:getAllPositions,
    :position,:savePositions,:getDepartments,:getSpecialOffer,:addPosition,:getAllPositionPrice,:addPositionPrice,
    :updatePositionPolicy,:getAllZjlyPrice,:addZjlyPrice,:editZjlyPrice,:getAllAirportComboBox,:editPolicy,:getAllPolicy,
    :getAllErrorInfo,:getPositionsBy]
  
  def zjlyPrice
    #权限
    if CommonHelper.rightAuthorization(session[:user_rights],params[:controller],params[:action]) == "false"
      redirect_to(:controller => "index", :action => "main") and return
    end 
  end
  
  def specialOffer
    #权限
    if CommonHelper.rightAuthorization(session[:user_rights],params[:controller],params[:action]) == "false"
      redirect_to(:controller => "index", :action => "main") and return
    end 
  end
  
  def parameter_setup
    #权限
    if CommonHelper.rightAuthorization(session[:user_rights],params[:controller],params[:action]) == "false"
      redirect_to(:controller => "index", :action => "main") and return
    end 
  end
  
  def plane_tax
  end
  
  def fuel_tax
  end
  
  def getAllFuelTaxs
    begin
      ids = ""
      strdelData = params[:delData]
      if strdelData!=nil
        results = ActiveSupport::JSON.decode(strdelData)
        results.each do |result|
          ids << "," if ids!=""
          ids << result["id"]
        end
        FuelTax.update_all("is_locked = 1","id in ("+ids+")")
      end
      fuelTaxs = FuelTax.find(:all,:conditions=>["is_locked <> 1"])
      jsonstr="{totalProperty:"+fuelTaxs.length.to_s+",root:"+fuelTaxs.to_json()+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  def addFuelTax
    begin
      fromto = params[:from]+params[:to]
      fuelTax = FuelTax.find(:first,:conditions=>["fromto=?",fromto])
      if fuelTax==nil
        fuelTax = FuelTax.new
        fuelTax.fromto = fromto
      end
      fuelTax.km = params[:km]
      fuelTax.tax = params[:tax]
      fuelTax.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  def editFuelTax
    begin
      fuelTax = FuelTax.find(params[:id])
      fuelTax.fromto = params[:fromto]
      fuelTax.km = params[:km]
      fuelTax.tax = params[:tax]
      fuelTax.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def getFuelTaxByID
    begin
      fuelTax = FuelTax.find(params[:id])
      jsonstr="{root:["+fuelTax.to_json()+"]}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  def getAllPlaneTypes
    begin
      strdelData = params[:delData]
      if strdelData!=nil
        delPlaneType(strdelData)
      end
      planeTypes = PlaneTax.find(:all,:conditions=>["is_locked <> 1"])
      jsonstr="{totalProperty:"+planeTypes.length.to_s+",root:"+planeTypes.to_json()+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  #Add PlaneTax
  def addPlaneTax
    begin
      planeTax = PlaneTax.new
      planeTax.plane_type=params[:plane_type]
      planeTax.tax=params[:tax]
      planeTax.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Delete PlaneTax
  def delPlaneTax(strdelData)
    ids=""
    begin
      results = ActiveSupport::JSON.decode(strdelData)
      results.each do |result|
        ids << result["id"]+","
      end
      ids = ids[0,ids.length-1]
      PlaneTax.update_all("is_locked = 1","id in ("+ids+")")
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  # Get PlaneTax By ID
  def getPlaneTaxByID
    begin
      planeTax = PlaneTax.find(params[:id])
      jsonstr="{root:["+planeTax.to_json()+"]}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Edit PlaneTax
  def editPlaneTax
    begin
      planeTax = PlaneTax.find(params[:id])
      planeTax.plane_type=params[:plane_type]
      planeTax.tax=params[:tax]
      planeTax.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #支付方式
  def payment_type
    
  end
  
  def getAllPaymemtTypes  
    begin
      strdelData = params[:delData]
      if strdelData!=nil
        delPaymentType(strdelData)
      end
      paymentTypes = PaymentType.find(:all,:conditions=>["is_locked <> 1"])
      jsonstr="{totalProperty:"+paymentTypes.length.to_s+",root:"+paymentTypes.to_json()+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Add PaymentType
  def addPaymentType
    begin
      paymentType = PaymentType.new
      paymentType.payment_type=params[:payment_type]
      paymentType.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Delete PaymentType
  def delPaymentType(strdelData)
    ids=""
    begin
      results = ActiveSupport::JSON.decode(strdelData)
      results.each do |result|
        ids << result["id"]+","
      end
      ids = ids[0,ids.length-1]
      PaymentType.update_all("is_locked = 1","id in ("+ids+")")
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  # Get PaymentType By ID
  def getPaymentTypeByID
    begin
      paymentType = PaymentType.find(params[:id])
      jsonstr="{root:["+paymentType.to_json()+"]}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Edit PaymentType
  def editPaymentType
    begin
      paymentType = PaymentType.find(params[:id])
      paymentType.payment_type=params[:payment_type]
      paymentType.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #客户类型
  def customer_type
    
  end
  
  def getAllCustomerTypes  
    begin
      strdelData = params[:delData]
      if strdelData!=nil
        delCustomerType(strdelData)
      end
      customerTypes = CustomerType.find(:all,:conditions=>["is_locked <> 1"])
      jsonstr="{totalProperty:"+customerTypes.length.to_s+",root:"+customerTypes.to_json()+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Add CustomerType
  def addCustomerType
    begin
      customerType = CustomerType.new
      customerType.customer_type = params[:customer_type]
      customerType.return_cash = params[:return_cash]
      customerType.return_rate = params[:return_rate].to_i
      customerType.plus = params[:plus]
      customerType.plus_rate = params[:plus_rate].to_i
      customerType.father_rate = params[:father_rate].to_i
      customerType.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Delete CustomerType
  def delCustomerType(strdelData)
    ids=""
    begin
      results = ActiveSupport::JSON.decode(strdelData)
      results.each do |result|
        ids << result["id"]+","
      end
      ids = ids[0,ids.length-1]
      CustomerType.update_all("is_locked = 1","id in ("+ids+")")
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  # Get CustomerType By ID
  def getCustomerTypeByID
    begin
      customerType = CustomerType.find(params[:id])
      jsonstr="{root:["+customerType.to_json()+"]}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Edit CustomerType
  def editCustomerType
    begin
      customerType = CustomerType.find(params[:id])
      customerType.customer_type = params[:customer_type]
      customerType.return_cash = params[:return_cash]
      customerType.return_rate = params[:return_rate].to_i
      customerType.plus = params[:plus]
      customerType.plus_rate = params[:plus_rate].to_i
      customerType.father_rate = params[:father_rate].to_i
      customerType.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #电话类型------------------------------------------------------------------------------------------------------------------
  def phone_type
    
  end
  
  def getAllPhoneTypes  
    begin
      strdelData = params[:delData]
      if strdelData!=nil
        delPhoneType(strdelData)
      end
      phoneTypes = PhoneType.find(:all,:conditions=>["is_locked <> 1"])
      jsonstr="{totalProperty:"+phoneTypes.length.to_s+",root:"+phoneTypes.to_json()+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Add PhoneType
  def addPhoneType
    begin
      phoneType = PhoneType.new
      phoneType.phone_type=params[:phone_type]
      phoneType.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Delete PhoneType
  def delPhoneType(strdelData)
    ids=""
    begin
      results = ActiveSupport::JSON.decode(strdelData)
      results.each do |result|
        ids << result["id"]+","
      end
      ids = ids[0,ids.length-1]
      PhoneType.update_all("is_locked = 1","id in ("+ids+")")
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  # Get PhoneType By ID
  def getPhoneTypeByID
    begin
      phoneType = PhoneType.find(params[:id])
      jsonstr="{root:["+phoneType.to_json()+"]}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Edit PhoneType
  def editPhoneType
    begin
      phoneType = PhoneType.find(params[:id])
      phoneType.phone_type=params[:phone_type]
      phoneType.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #证件类型------------------------------------------------------------------------------------------------------------------
  def card_type
    
  end
  
  def getAllCardTypes  
    begin
      strdelData = params[:delData]
      if strdelData!=nil
        delCardType(strdelData)
      end
      cardTypes = CardType.find(:all,:conditions=>["is_locked <> 1"])
      jsonstr="{totalProperty:"+cardTypes.length.to_s+",root:"+cardTypes.to_json()+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Add CardType
  def addCardType
    begin
      cardType = CardType.new
      cardType.card_type=params[:card_type]
      cardType.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Delete CardType
  def delCardType(strdelData)
    ids=""
    begin
      results = ActiveSupport::JSON.decode(strdelData)
      results.each do |result|
        ids << result["id"]+","
      end
      ids = ids[0,ids.length-1]
      CardType.update_all("is_locked = 1","id in ("+ids+")")
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  # Get CardType By ID
  def getCardTypeByID
    begin
      cardType = CardType.find(params[:id])
      jsonstr="{root:["+cardType.to_json()+"]}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  #Edit CardType
  def editCardType
    begin
      cardType = CardType.find(params[:id])
      cardType.card_type=params[:card_type]
      cardType.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  #结算类型------------------------------------------------------------------------------------------------------------------
  def settlement_type
    
  end
  
  def getAllSettlementTypes  
    begin
      strdelData = params[:delData]
      if strdelData!=nil
        delSettlementType(strdelData)
      end
      settlementTypes = SettlementType.find(:all,:conditions=>["is_locked <> 1"])
      jsonstr="{totalProperty:"+settlementTypes.length.to_s+",root:"+settlementTypes.to_json()+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Add SettlementType
  def addSettlementType
    begin
      settlementType = SettlementType.new
      settlementType.settlement_type=params[:settlement_type]
      settlementType.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Delete SettlementType
  def delSettlementType(strdelData)
    ids=""
    begin
      results = ActiveSupport::JSON.decode(strdelData)
      results.each do |result|
        ids << result["id"]+","
      end
      ids = ids[0,ids.length-1]
      SettlementType.update_all("is_locked = 1","id in ("+ids+")")
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  # Get SettlementType By ID
  def getSettlementTypeByID
    begin
      settlementType = SettlementType.find(params[:id])
      jsonstr="{root:["+settlementType.to_json()+"]}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Edit SettlementType
  def editSettlementType
    begin
      settlementType = SettlementType.find(params[:id])
      settlementType.settlement_type=params[:settlement_type]
      settlementType.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #消费级别------------------------------------------------------------------------------------------------------------------
  def cost_level
    
  end
  
  def getAllCostLevels  
    begin
      strdelData = params[:delData]
      if strdelData!=nil
        delCostLevel(strdelData)
      end
      costLevels = CostLevel.find(:all,:conditions=>["is_locked <> 1"])
      jsonstr="{totalProperty:"+costLevels.length.to_s+",root:"+costLevels.to_json()+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Add CostLevel
  def addCostLevel
    begin
      costLevel = CostLevel.new
      costLevel.cost_level=params[:cost_level]
      costLevel.consumption=params[:consumption]
      costLevel.remark=params[:remark]
      costLevel.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Delete CostLevel
  def delCostLevel(strdelData)
    ids=""
    begin
      results = ActiveSupport::JSON.decode(strdelData)
      results.each do |result|
        ids << result["id"]+","
      end
      ids = ids[0,ids.length-1]
      CostLevel.update_all("is_locked = 1","id in ("+ids+")")
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  # Get CostLevel By ID
  def getCostLevelByID
    begin
      
      costLevel = CostLevel.find(params[:id])
      jsonstr="{root:["+costLevel.to_json()+"]}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  #Edit CostLevel
  def editCostLevel
    begin
      
      costLevel = CostLevel.find(params[:id])
      costLevel.cost_level=params[:cost_level]
      costLevel.consumption=params[:consumption]
      costLevel.remark=params[:remark]
      costLevel.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  #航空公司------------------------------------------------------------------------------------------------------------------
  def airways
    
  end
  
  def getAllAirways  
    begin
      strdelData = params[:delData]
      if strdelData!=nil
        delAirways(strdelData)
      end
      airways = Airways.find(:all,:conditions=>["is_locked <> 1"])
      jsonstr="{totalProperty:"+airways.length.to_s+",root:"+airways.to_json()+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  #得到所有国内航空公司
  def getInternalAirways  
    begin
      puts "dfasdfaasdd"
      airways = Airways.find(:all,:conditions=>["is_locked <> 1 AND airways_type = 'internal'"])
      jsonstr="{totalProperty:"+airways.length.to_s+",root:"+airways.to_json()+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end

  #Add Airways
  def addAirways
    begin
      airways = Airways.new
      airways.airways_name=params[:airways_name]
      airways.airways_code=params[:airways_code]
      airways.passenger_code=params[:passenger_code]
      airways.airways_type=params[:airways_type]
      airways.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Delete Airways
  def delAirways(strdelData)
    ids=""
    begin
      
      results = ActiveSupport::JSON.decode(strdelData)
      results.each do |result|
        ids << result["id"]+","
      end
      ids = ids[0,ids.length-1]
      Airways.update_all("is_locked = 1","id in ("+ids+")")
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  # Get Airways By ID
  def getAirwaysByID
    begin
      airways = Airways.find(params[:id])
      jsonstr="{root:["+airways.to_json()+"]}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Edit Airways
  def editAirways
    begin
      airways = Airways.find(params[:id])
      airways.airways_name=params[:airways_name]
      airways.airways_code=params[:airways_code]
      airways.passenger_code=params[:passenger_code]
      airways.airways_type=params[:airways_type]
      airways.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #空港管理------------------------------------------------------------------------------------------------------------------
  def airport
    
  end
  
  def getAllAirports
    begin
      strdelData = params[:delData]
      if strdelData!=nil
        delAirport(strdelData)
      end
      queryKey =  params[:query]
      airport = nil
      if queryKey != nil
        airport = Airport.find(:all,:conditions=>["is_locked <> 1 AND airport_code like ? OR py like ?","%#{queryKey}%","%#{queryKey}%"])
      else
        airport = Airport.find(:all,:conditions=>["is_locked <> 1"])
      end
      jsonstr="{totalProperty:"+airport.length.to_s+",root:"+airport.to_json()+"}"
      
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Add Airport
  def addAirport
    begin
      airport = Airport.new
      airport.airport_name=params[:airport_name]
      airport.airport_code=params[:airport_code]
      airport.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Delete Airport
  def delAirport(strdelData)
    ids=""
    begin
      results = ActiveSupport::JSON.decode(strdelData)
      results.each do |result|
        ids << result["id"]+","
      end
      ids = ids[0,ids.length-1]
      Airport.update_all("is_locked = 1","id in ("+ids+")")
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  # Get Airport By ID
  def getAirportByID
    begin
      airport = Airport.find(params[:id])
      jsonstr="{root:["+airport.to_json()+"]}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Edit Airport
  def editAirport
    begin
      
      airport = Airport.find(params[:id])
      airport.airport_name=params[:airport_name]
      airport.airport_code=params[:airport_code]
      airport.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  #空港管理------------------------------------------------------------------------------------------------------------------
  def department
    
  end
  
  def getAllDepartments  
    begin
      
      strdelData = params[:delData]
      if strdelData!=nil
        delDepartment(strdelData)
      end
      department = Department.find(:all,:conditions=>["is_locked <> 1"])
      jsonstr="{totalProperty:"+department.length.to_s+",root:"+department.to_json()+"}"
      
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  #Add Department
  def addDepartment
    begin
      
      department = Department.new
      department.name=params[:name]
      department.area=params[:area]
      department.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  #Delete Department
  def delDepartment(strdelData)
    ids=""
    begin
      
      results = ActiveSupport::JSON.decode(strdelData)
      results.each do |result|
        ids << result["id"]+","
      end
      ids = ids[0,ids.length-1]
      Department.update_all("is_locked = 1","id in ("+ids+")")
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  # Get Department By ID
  def getDepartmentByID
    begin
      
      department = Department.find(params[:id])
      jsonstr="{root:["+department.to_json()+"]}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  #Edit Department
  def editDepartment
    begin
      department = Department.find(params[:id])
      department.name=params[:name]
      department.area=params[:area]
      department.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  def getCustomerTypes
    begin
      
      customerTypes=CustomerType.find(:all,:conditions=>["is_locked <> 1"])
      jsonstr="{root:"+customerTypes.to_json()+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def getCardTypes
    begin
      
      cardTypes=CardType.find(:all,:conditions=>["is_locked <> 1"])
      jsonstr="{root:"+cardTypes.to_json()+"}"
      
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  def getAllPositions
    begin
      
      strdelData = params[:delData]
      if strdelData!=nil
        delPosition(strdelData)
      end
      positions=Position.find(:all,:conditions=>["is_locked <> 1"])
      jsonstr="{root:"+positions.to_json(:include=>[:airways])+"}"
      
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  def delPosition(strdelData)
    ids=""
    begin
      
      results = ActiveSupport::JSON.decode(strdelData)
      results.each do |result|
        ids << result["id"]+","
      end
      ids = ids[0,ids.length-1]
      Position.update_all("is_locked = 1","id in ("+ids+")")
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  def addPosition
    begin
      position = Position.new
      position.airways_id=params[:airways_name_id]
      position.position_code=params[:position_code]
      position.discount=params[:discount]
      position.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def savePositions
    begin
      
      strEditData = params[:json]
      results = ActiveSupport::JSON.decode(strEditData)
      results.each do |result|
        id=result["id"]
        position_code=result["position_code"]
        discount=result["discount"]
        Position.update(id,:position_code=>result["position_code"],:discount=>result["discount"] )
      end
      jsonstr="{success:true}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  def getPositionsBy
    begin
      rootStr = "["
      air_no = params[:air_no]
      airways_code = params[:airways_code]
      airways_code = air_no[0,2] if air_no && air_no.length>2
      fromto = params[:fromto]
      airways = Airways.find_by_code(airways_code) if airways_code
      if airways
        priceHash = CommonHelper.getPositionPriceHash(air_no,fromto)
        #posirions = airways.positions
        positions = Position.find(:all,:conditions=>["is_locked <> 1 and airways_id=?",airways.id])
        for position in positions
          rootStr << "," if rootStr!="["
          if priceHash[position.position_code]
            rootStr << "{'position':'"+position.position_code+"','price':'"+priceHash[position.position_code].price.to_s+"'}"
          else
            rootStr << "{'position':'"+position.position_code+"','price':''}"
          end
        end
      end
      rootStr << "]"
      jsonstr="{root:"+rootStr+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def getDepartments
    begin
      departments=Department.find(:all)
      jsonstr="{root:"+departments.to_json()+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  def getSpecialOffer
    begin
      specialoffer=SpecialOffer.find(:first)
      jsonstr="{root:["+specialoffer.to_json()+"]}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  def setSpecialOffer
    begin
      specialoffer=SpecialOffer.find(:first)
      specialoffer.content = params[:content]
      specialoffer.save
      jsonstr="SUCCESS"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def getAllPositionPrice
    begin
      strdelData = params[:delData]
      if strdelData!=nil
        ids = ""
        results = ActiveSupport::JSON.decode(strdelData)
        results.each do |result|
          ids << "," if ids!=""
          ids << result["id"]
        end
        PositionPrice.update_all("is_locked = 1","id in ("+ids+")")
      end
      fromto = params[:fromto]
      airways_code = params[:airways_code]
      special = params[:special]
      
      if special && special=="yes"
        sql = "special='yes'"
      else
        sql = "(special is null or special='' or special='no')"
      end
      if fromto && fromto!=""
        sql << " and fromto='"+fromto+"'"
      end
      if airways_code && airways_code!=""
        sql << " and airways_code='"+airways_code+"'"
      end
      sql << " AND is_locked <> 1"
      logger.error "sql="+sql
      ps = PositionPrice.find(:all,:conditions=>sql,:order=>"fromto")
      rootstr = ps.to_json()
      jsonstr = "{root:"+rootstr+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  def addPositionPrice
    begin
      
      from = params[:from]
      to = params[:to]
      airways_code = params[:airways_code]
      airNo = params[:air_no]
      position = params[:position]
      start_date = params[:start_date]
      end_date = params[:end_date]
      checkbox_special = params[:checkbox_special]
      price = params[:price]
      policy = params[:policy]
      
      logger.error from
      logger.error to
      logger.error airways_code
      logger.error airNo
      logger.error position
      logger.error start_date
      logger.error checkbox_special
      logger.error price
      logger.error policy
      
      #如果操作员只输入航班号中的数字，则自动在数字前面加上航空公司代码
      if airNo.to_i>0
        airNo = airways_code+airNo
      end
      
      result = "{success:false}"
      pp = PositionPrice.new
      pp.airways_code = airways_code
      pp.air_no = airNo
      pp.fromto = from+to
      pp.position = position
      pp.price = price.to_i if price
      pp.policy = policy
      if checkbox_special
        pp.special = "yes"
      end
      pp.start_date = start_date if start_date && start_date!=""
      pp.end_date = end_date if end_date && end_date!=""
      if pp.save
        result = "{success:true}"
        logger.error pp.id
      end
      logger.error "result="+result
      render :text=>result, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  #编辑退改签政策
  def updatePositionPolicy
    begin
      ids = params[:ids]
      policy = params[:policy]
      PositionPrice.update_all("policy='"+policy+"'","id in("+ids+")")
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  def getAllZjlyPrice
    begin
      strdelData = params[:delData]
      if strdelData!=nil
        ids = ""
        results = ActiveSupport::JSON.decode(strdelData)
        results.each do |result|
          ids << "," if ids!=""
          ids << result["id"]
        end
        ZjlyPrice.delete_all("id in ("+ids+")")
        #ZjlyPrice.update_all("is_locked = 1","id in ("+ids+")")
      end
      
      zs = ZjlyPrice.find(:all,:conditions=>"is_locked <> 1")
      for zjly in zs
        airport = Airport.new()
        airport.airport_name = ""
        if zjly.mid_airport2==nil
          zjly.mid_airport2 = airport
        end
        if zjly.mid_airport3==nil
          zjly.mid_airport3 = airport
        end
        if zjly.mid_airport4==nil
          zjly.mid_airport4 = airport
        end
        if zjly.end_airport==nil
          zjly.end_airport = airport
        end
      end
      rootstr = zs.to_json(:include=>[:from_airport,:mid_airport,:mid_airport2,:to_airport,:mid_airport3,:mid_airport4,:end_airport,:airways])
      jsonstr = "{root:"+rootstr+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def getZjlyPriceByID
    begin
      id = params[:id]
      zjly = ZjlyPrice.find_by_id(id)
      airport = Airport.new()
      airport.airport_name = ""
      if zjly.mid_airport2==nil
        zjly.mid_airport2 = airport
      end
      if zjly.mid_airport3==nil
        zjly.mid_airport3 = airport
      end
      if zjly.mid_airport4==nil
        zjly.mid_airport4 = airport
      end
      if zjly.end_airport==nil
        zjly.end_airport = airport
      end
      rootstr = zjly.to_json(:include=>[:from_airport,:mid_airport,:mid_airport2,:to_airport,:mid_airport3,:mid_airport4,:end_airport,:airways])
      jsonstr = "{root:["+rootstr+"]}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def addZjlyPrice
    begin
      from_id = params[:from_id]
      mid_id = params[:mid_id]
      mid_id2 = params[:mid_id2]
      mid_id3 = params[:mid_id3]
      mid_id4 = params[:mid_id4]
      end_id = params[:end_id]
      to_id = params[:to_id]
      airways_id = params[:airways]
      start_date = params[:start_date]
      end_date = params[:end_date]
      position = params[:position].upcase
      position2 = params[:position2].upcase
      position3 = params[:position3]
      position4 = params[:position4]
      price = params[:price]
      price2 = params[:price2]
      rake_off = params[:rake_off]
      policy = params[:policy]
      trip_type = params[:trip_type]
      is_closed = params[:is_closed]
      position2 = position if position2==""
      position3 = position3.upcase if position3 && position3!=""
      position4 = position4.upcase if position4 && position4!=""
#      logger.error "======= addZjlyPrice ========="
#      logger.error from_id
#      logger.error mid_id
#      logger.error mid_id2
#      logger.error to_id
#      logger.error mid_id3
#      logger.error mid_id4
#      logger.error end_id
#      logger.error airways_id
#      logger.error start_date
#      logger.error end_date
#      logger.error position
#      logger.error position2
#      logger.error position3
#      logger.error position4
#      logger.error price
#      logger.error price2
#      logger.error rake_off
#      logger.error policy
#      logger.error trip_type
#      logger.error is_closed

      if from_id==to_id && end_id==""
        trip_type=="往返"
      end
      result = "{success:false}"
      sql = " and is_locked<>1 "
      if trip_type=="联程" || trip_type=="往返"
        sql << " and from_id="+from_id+" and mid_id="+mid_id+" and to_id="+to_id
        sql << " and mid_id2="+mid_id2 if mid_id2!=""
      elsif mid_id3!="" && end_id!=""
        sql << " and mid_id3="+mid_id3+" and end_id="+end_id
        sql << " and mid_id4="+mid_id4 if mid_id4!=""
      end
      if start_date!="" && end_date!=""
        sql << " and ((start_date<='"+start_date+"' and end_date>='"+start_date+"') or (start_date<='"+end_date+"' and end_date>='"+end_date+"'))"
      elsif start_date!=""
        sql << " and start_date<='"+start_date+"' and end_date>='"+start_date+"'"
      elsif end_date!=""
        sql << " or start_date<='"+end_date+"' and end_date>='"+end_date+"'"
      end
      #logger.error sql
      if ZjlyPrice.find(:first,:conditions=>["trip_type=? and position=? and position2=? and airways_id=? "+sql,trip_type,position,position2,airways_id])
        result = "{success:true,info:'已有日期重叠的该航空公司该行段该仓位的特价信息'}"
      else
        re = ZjlyPrice.saveZjlyPrice(nil,from_id,mid_id,mid_id2,to_id,mid_id3,mid_id4,end_id,airways_id,start_date,end_date,price,position,position2,position3,position4,trip_type,rake_off,is_closed,policy)
        #logger.error re
        if re && re>0
          result = "添加成功"
          if price2 && price2.to_i>0 && trip_type=="联程"
            trip_type = "联程往返"
            re = ZjlyPrice.saveZjlyPrice(nil,from_id,mid_id,mid_id2,to_id,mid_id,mid_id2,from_id,airways_id,start_date,end_date,price2,position,position2,position2,position,trip_type,rake_off,is_closed,policy)
            if re==nil
              result += ",但是保存往返价格记录失败"
            end
          end
          #logger.error result
          result = "{success:true,info:'"+result+"'}"
        else
          result = "{success:true,info:'保存记录失败'}"
        end
      end
      render :text=>result, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def editZjlyPrice
    begin
      id = params[:id]
      from_id = params[:from_id]
      mid_id = params[:mid_id]
      mid_id2 = params[:mid_id2]
      mid_id3 = params[:mid_id3]
      mid_id4 = params[:mid_id4]
      end_id = params[:end_id]
      to_id = params[:to_id]
      airways_id = params[:airways]
      start_date = params[:start_date]
      end_date = params[:end_date]
      position = params[:position].upcase
      position2 = params[:position2].upcase
      position3 = params[:position3]
      position4 = params[:position4]
      price = params[:price]
      price2 = params[:price2]
      rake_off = params[:rake_off]
      policy = params[:policy]
      trip_type = params[:trip_type]
      is_closed = params[:is_closed]
      position2 = position if position2==""

#      logger.error "======= editZjlyPrice ========="
#      logger.error id
#      logger.error from_id
#      logger.error mid_id
#      logger.error mid_id2
#      logger.error to_id
#      logger.error mid_id3
#      logger.error mid_id4
#      logger.error end_id
#      logger.error airways_id
#      logger.error start_date
#      logger.error end_date
#      logger.error position
#      logger.error position2
#      logger.error position3
#      logger.error position4
#      logger.error price
#      logger.error price2
#      logger.error rake_off
#      logger.error policy
#      logger.error trip_type
#      logger.error is_closed
      
      if from_id && from_id!="" && from_id==to_id && end_id==""
        trip_type = "往返"
      end

      re = ZjlyPrice.saveZjlyPrice(id,from_id,mid_id,mid_id2,to_id,mid_id3,mid_id4,end_id,airways_id,start_date,end_date,price,position,position2,position3,position4,trip_type,rake_off,is_closed,policy)
      #logger.error re
      if re && re>0
        result = "{success:true,info:'编辑成功'}"
      else
        result = "{success:true,info:'保存记录失败'}"
      end
      render :text=>result, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def getAllAirportComboBox
    begin
      query = params[:query]
      #logger.error query
      airports = Airport.find(:all,:conditions=>[" airport_name like ? or airport_code like ? or py like ?","%#{query}%","%#{query}%","%#{query}%"],:order=>'name_index')
      #logger.error airports.length
      rootStr = airports.to_json()
      #logger.error rootStr
      jsonstr="{root:"+rootStr+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def policyManager
    #权限
    if CommonHelper.rightAuthorization(session[:user_rights],params[:controller],params[:action]) == "false"
      redirect_to(:controller => "index", :action => "main") and return
    end 
    begin
      
      @airports_arraystr = getAirPortArrayString
      logger.error @airports_arraystr
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  def getAirPortArrayString
    begin
      airports = Airport.find(:all,:order => "name_index")
      airports_arraystr="["
      airports.each do |airport|
        airports_arraystr << "[\"#{airport.id}|#{airport.airport_name}\",\"#{airport.airport_name}\"],"
      end
      if airports_arraystr.length>1
        airports_arraystr = airports_arraystr[0,airports_arraystr.length-1]
      end
      airports_arraystr  << "]"
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  def getAllPolicy
    begin
      pageBegin =  params[:start]
      pageEnd =  params[:limit]
      dateBegin = params[:date_begin]
      dateEnd = params[:date_end]
      airwaysID = params[:airways_id]
      strdelData = params[:delData]
      if strdelData!=nil
        delPolicy(strdelData)
      end
      if dateBegin != nil && dateBegin !=""
        if airwaysID != ""
          policies = Policy.find(:all,:conditions=>["is_locked <> 1 AND date_begin >= ? AND date_end <= ? AND airways_id = ?",dateBegin,dateEnd,airwaysID],:limit =>pageBegin+","+pageEnd)
        else
          policies = Policy.find(:all,:conditions=>["is_locked <> 1 AND date_begin >= ? AND date_end <= ?",dateBegin,dateEnd],:limit =>pageBegin+","+pageEnd)
        end
      else
        policies = Policy.find(:all,:conditions=>["is_locked <> 1"],:limit =>pageBegin+","+pageEnd)
      end
      jsonstr="{root:"+policies.to_json(:include=>[:user,:airways,:airport_from])+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  def delPolicy(strdelData)
    ids=""
    begin
      results = ActiveSupport::JSON.decode(strdelData)
      results.each do |result|
        ids << result["id"]+","
      end
      ids = ids[0,ids.length-1]
      Policy.update_all("is_locked = 1","id in ("+ids+")")
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  def addPolicy
    begin
      logger.error params
      policy = Policy.new
      policy.user_id = session[:user_id]
      policy.airways_id = params[:airways_id]
      #webPriceRelease.airport_from = params[:airport_from]
      policy.airport_from_id = params[:airport_from_id]
      policy.flights = params[:flights]
      policy.position = params[:position]
      policy.date_begin = params[:date_begin]
      policy.date_end = params[:date_end]
      policy.airports_to = params[:airports]
      policy.rake_off = params[:rake_off]
      policy.airports_to = params[:airports]
      policy.trip_type = params[:trip_type]
      airportsArray = params[:airports].split(",")
      airportsStr = ""
      airportHash = Airport.findByIDHash
      fromAirportCode = airportHash[policy.airport_from_id].airport_code
      fromtoStr = ""
      0.upto(airportsArray.length-1) do |i|
        fromtoStr << "#{fromAirportCode}#{airportHash[airportsArray[i].split("|")[0].to_i].airport_code}|"
        airportsArray[i] = airportsArray[i].split("|")[1]
        airportsStr = airportsStr + airportsArray[i] + "|"
      end
      logger.error fromtoStr
      policy.from_to = fromtoStr
      policy.airports_to_desc = airportsStr[0,airportsStr.length-1]
      policy.save
      info = "政策添加成功"
      jsonstr="{success:true,info:'#{info}'}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def editPolicy
    begin
      policy = Policy.find(params[:id])
      @id = params[:id]
      #选择框的右边
      @airports_selected_arraystr = "["

      airportsToArray = policy.airports_to.split(',')
      ids=""
      airportsToArray.each do |airports_selected|
        @airports_selected_arraystr << "[\"#{airports_selected.split('|')[0]}|#{airports_selected.split('|')[1]}\",\"#{airports_selected.split('|')[1]}\"],"
        ids << "#{airports_selected.split('|')[0]},"
      end
      ids=ids[0,ids.length-1]
      @airports_selected_arraystr = @airports_selected_arraystr[0,@airports_selected_arraystr.length-1]+"]"
      airports = Airport.find(:all,:conditions=>["is_locked <> 1 AND id not in ("+ids+")"],:order => "name_index")
      logger.error airports.size
      @airports_arraystr="["
      airports.each do |airport|
        @airports_arraystr << "[\"#{airport.id}|#{airport.airport_name}\",\"#{airport.airport_name}\"],"
      end
      if @airports_arraystr.length>1
        @airports_arraystr = @airports_arraystr[0,@airports_arraystr.length-1]
      end
      @airports_arraystr << "]"
      #jsonstr="{root:["+policy.to_json(:include=>[:airways,:airport_from])+"]}"
      #logger.error "--------------fromData---------------"
      #logger.error policy.fromData
      #logger.error "---------------toData----------------"
      #logger.error policy.toData
      chars = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,"
      @positionStr = "["
      positionSelectedArray = policy.position
      if policy.position.length >0
        @positionSelected = "["
        positionSelectedArray = policy.position.split(',')
        positionSelectedArray.each do |positionItem|
          @positionSelected << "['#{positionItem}','#{positionItem}'],"
          chars = chars.gsub("#{positionItem},","") 
        end
        @positionSelected = @positionSelected[0,@positionSelected.length-1]
        @positionSelected << "]"
      else
        @positionSelected = "[]"
      end
      
      positionArray = chars.split(",")
      positionArray.each do |positionItem|
        @positionStr << "['#{positionItem}','#{positionItem}'],"
      end
      if @positionStr.length>1
        @positionStr = @positionStr[0,@positionStr.length-1]
      end
      @positionStr << "]"
      #logger.error @positionStr
      #logger.error @positionSelected
      #render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def savePolicy
    begin
      policy = Policy.find(params[:id])
      policy.user_id = session[:user_id]
      #webPriceRelease.airport_from = params[:airport_from]
      policy.flights = params[:flights]
      policy.position = params[:position]
      policy.date_begin = params[:date_begin]
      policy.date_end = params[:date_end]
      policy.airports_to = params[:airports]
      policy.rake_off = params[:rake_off]
      policy.airports_to = params[:airports]
      policy.trip_type = params[:trip_type]
      
      if params[:airport_from_id] !=nil && params[:airport_from_id] !=""
        policy.airport_from_id = params[:airport_from_id]
      end
      if params[:airways_id] !=nil && params[:airways_id] !=""
        policy.airways_id = params[:airways_id]
      end
      airportsArray = params[:airports].split(",")
      airportsStr = ""
      airportHash = Airport.findByIDHash
      fromAirportCode = airportHash[policy.airport_from_id].airport_code
      fromtoStr = ""
      0.upto(airportsArray.length-1) do |i|
        fromtoStr << "#{fromAirportCode}#{airportHash[airportsArray[i].split("|")[0].to_i].airport_code}|"
        airportsArray[i] = airportsArray[i].split("|")[1]
        airportsStr << airportsArray[i] + "|"
      end
      policy.from_to = fromtoStr
      policy.airports_to_desc = airportsStr[0,airportsStr.length-1]
      policy.save
      info = "政策编辑成功"
      jsonstr="{success:true,info:'#{info}'}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
    
  def getPolicyByID
    begin
      policy = Policy.find(params[:id])
      jsonstr="{root:["+policy.to_json(:include=>[:airways,:airport_from])+"]}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def errorInfoList
    
  end
  
  def getAllErrorInfo
    begin
      log_level = params[:log_level]
      if log_level == "true"
        log_level = "error"
      else
        log_level = "info"
      end
      pageBegin =  params[:start]
      pageEnd =  params[:limit]
      strdelData = params[:delData]
      if strdelData!=nil
        delErrorInfo(strdelData)
      end
      #for search
      searchType=params[:search_type]
      searchKey =params[:search_key]
      allErrorInfos = ErrorInfo.find(:all,:select=>"id")
      if searchType == nil
        errorInfos = ErrorInfo.find(:all,:limit =>pageBegin+","+pageEnd,:order=>"created_at desc")
      else
        searchKey = "%"+searchKey+"%"
        errorInfos = ErrorInfo.find(:all,:conditions=>[searchType + " like ? AND log_level = ?",searchKey,log_level],:limit =>pageBegin+","+pageEnd,:order=>"created_at desc")
      end
      jsonstr="{totalProperty:"+allErrorInfos.length.to_s+",root:"+errorInfos.to_json(:include=>[:user])+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def delErrorInfo(strdelData)
    ids=""
    begin
      results = ActiveSupport::JSON.decode(strdelData)
      results.each do |result|
        ids << result["id"]+","
      end
      ids = ids[0,ids.length-1]
      ErrorInfo.delete_all("id in ("+ids+")")
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
end
